//
//  XCHotSongListViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/16.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCHotSongListViewController.h"
#import "XCShareMusicTableViewCell.h"
#import "XCAFNetTool.h"
#import "UIImageView+WebCache.h"
#import "OnlineAVplay.h"
#import "XCUrlManger.h"
#import "XCHotSixSongListModel.h"
#import "XCNewSongNewDieModel.h"
#import "MBProgressHUD+NJ.h"
#import "XCPlayViewController.h"
@interface XCHotSongListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSString *value;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UIImageView *hotSongListImageView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) NSMutableArray *dataSource1;

@property (nonatomic, assign) NSInteger section;

@property (nonatomic, retain) NSMutableArray *mutableArraySongName;

@property (nonatomic, retain) NSMutableArray *mutableArraySingerNAme;

@property (nonatomic, retain) NSMutableArray *nameArray;

@property (nonatomic, retain) NSMutableArray *mutableHeadIVPicurl;

@property (nonatomic, retain) NSMutableArray *mutableArraySongName1;

@property (nonatomic, retain) NSMutableArray *mutableArraySingerNAme1;

@property (nonatomic, retain) NSMutableArray *nameArray1;

@property (nonatomic, retain) NSMutableArray *mutableHeadIVPicurl1;

@end

@implementation XCHotSongListViewController

- (void)dealloc {
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
    
    [_tableView release];
    [_value release];
    [_hotSongListImageView release];
    [_mutableArraySongName release];
    [_mutableHeadIVPicurl release];
    [_nameArray release];
    
    
    [_mutableArraySongName1 release];
    [_mutableHeadIVPicurl1 release];
    [_nameArray1 release];
    
    [super dealloc];
}
//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    if ([OnlineAVplay shareAVPlayer].isPlay == YES) {
//        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 100) style:UITableViewStylePlain];
//    }
//    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//}
- (instancetype)initWithValue:(NSString *)value WithSection:(NSInteger)section
{
    self = [super init];
    if (self) {
        self.value = value;
        self.section = section;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    if (self.section == 2 || self.section == 3) {
        self.navigationItem.title = @"美妙音乐";
        [self HotSongListNetRequest];
        
    } else if (self.section == 5) {
        self.navigationItem.title = @"新歌新曲";
        [self HotSongListNetRequest2];
    }
    
    
    [self HotSongListTable];
    
    self.mutableArraySongName = [NSMutableArray arrayWithCapacity:0];
    self.mutableHeadIVPicurl = [NSMutableArray arrayWithCapacity:0];
    self.mutableArraySingerNAme = [NSMutableArray arrayWithCapacity:0];
    self.nameArray = [NSMutableArray arrayWithCapacity:0];
    
    self.mutableArraySongName1 = [NSMutableArray arrayWithCapacity:0];
    self.mutableHeadIVPicurl1 = [NSMutableArray arrayWithCapacity:0];
    self.mutableArraySingerNAme1 = [NSMutableArray arrayWithCapacity:0];
    self.nameArray1 = [NSMutableArray arrayWithCapacity:0];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
//    self.tableView.backgroundColor = [UIColor clearColor];
}
- (void)HotSongListNetRequest2 {
    
    self.dataSource = [NSMutableArray arrayWithCapacity:0];

    [XCAFNetTool GETNetWithUrl:[XCUrlManger NewSongNewDiNetRequest:self.value] body:nil headerFile:nil response:XCJSON success:^(id result) {
//        菊花
        [MBProgressHUD showMessage:@"正在加载"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            
        });
        
        XCNewSongNewDieModel *newSongNewDieModel = [[XCNewSongNewDieModel alloc] initWithDataSource:result[@"data"]];
        
        for (XCInTheSongList *intheSongList in newSongNewDieModel.songList) {
           
            [self.dataSource addObject:intheSongList];
            
        }
        for (int i = 0; i< self.dataSource.count; i++) {
            [self.mutableHeadIVPicurl addObject:newSongNewDieModel.singerPicUrl];
        }
        
        NSString *string = [NSString stringWithFormat:@"Image-%u",arc4random() % 10 ];
        self.hotSongListImageView.image = [UIImage imageNamed:string];
        
        
        [self.tableView reloadData];
    
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络错误"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            
        });
    }];
    
}
- (void)HotSongListNetRequest {
    
    [XCAFNetTool GETNetWithUrl:[XCUrlManger HotSongListNetRequest:self.value] body:nil headerFile:nil response:XCJSON success:^(id result) {
//        菊花
        [MBProgressHUD showMessage:@"正在加载"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            
        });
        
        self.dataSource1 = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result[@"songs"]) {
            XCHotSixSongListModel *hotSixSongListModel = [[XCHotSixSongListModel alloc] initWithDataSource:dic];
            
            if (hotSixSongListModel.picUrl != nil && hotSixSongListModel.urlList.count != 0 && hotSixSongListModel.name != nil && hotSixSongListModel.singerName != nil) {
                [self.dataSource1 addObject:hotSixSongListModel];
            }
            
        }

        XCHotSixSongListModel *hotSixSongListModel = self.dataSource1[arc4random() % 5 + 1];
        [self.hotSongListImageView sd_setImageWithURL:[NSURL URLWithString:hotSixSongListModel.picUrl]placeholderImage:[UIImage imageNamed:@"Image-p"]];
        
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络错误"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            
        });
    }];
    
}
- (void)HotSongListTable {
//    if ([OnlineAVplay shareAVPlayer].isPlay == YES) {
//       self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high) style:UITableViewStylePlain];
//    }
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, high / 4)];
    self.hotSongListImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -150, wide, high / 4 + 100)];
    [view addSubview:self.hotSongListImageView];
    
    self.tableView.tableHeaderView = view;
    
    [self.tableView registerClass:[XCShareMusicTableViewCell class] forCellReuseIdentifier:@"XCShareMusicTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
    [view release];
    [_tableView release];
    [_hotSongListImageView release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.section == 2 || self.section == 3) {
        XCHotSixSongListModel *hotSixSongListModel = self.dataSource1[indexPath.row];
        [[OnlineAVplay shareAVPlayer] playWithUrl:[hotSixSongListModel.urlList[0] objectForKey:@"url"]];

        for (XCHotSixSongListModel *hotSixSongListModel in self.dataSource1) {
            [self.mutableHeadIVPicurl1 addObject:hotSixSongListModel.picUrl];
            [self.mutableArraySongName1 addObject:[hotSixSongListModel.urlList[0] objectForKey:@"url"]];
            [self.mutableArraySingerNAme1 addObject:hotSixSongListModel.singerName];
            [self.nameArray1 addObject:hotSixSongListModel.name];
            
        }
        
        [XCPlayViewController sharePlayViewController].selectIndex = indexPath.row ;
        [XCPlayViewController sharePlayViewController].mutableArraySongName = self.mutableArraySongName1;
        
        [XCPlayViewController sharePlayViewController].mutableHeadIVPicurl = self.mutableHeadIVPicurl1;
        
        [XCPlayViewController sharePlayViewController].nameArray = self.nameArray1;
        [XCPlayViewController sharePlayViewController].muatbleArraySingerName = self.mutableArraySingerNAme1;
        [[OnlineAVplay shareAVPlayer] play];
        [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
            
        }];
        [OnlineAVplay shareAVPlayer].isPlay = YES;
        
    } else {
        
        for (XCInTheSongList *intheSongList in self.dataSource) {
            [self.mutableArraySongName addObject:[intheSongList.auditionList[0] objectForKey:@"url"]];
            [self.mutableArraySingerNAme addObject:intheSongList.singerName];
            [self.nameArray addObject:intheSongList.name];
 
    }
        [XCPlayViewController sharePlayViewController].selectIndex = indexPath.row ;
        [XCPlayViewController sharePlayViewController].mutableArraySongName = self.mutableArraySongName;
        [XCPlayViewController sharePlayViewController].mutableHeadIVPicurl = self.mutableHeadIVPicurl;
        [XCPlayViewController sharePlayViewController].nameArray = self.nameArray;
        [XCPlayViewController sharePlayViewController].muatbleArraySingerName = self.mutableArraySingerNAme;
        
        XCInTheSongList *intheSongList = self.dataSource[indexPath.row];
        [[OnlineAVplay shareAVPlayer] playWithUrl:[intheSongList.auditionList[0] objectForKey:@"url"]];
        [[OnlineAVplay shareAVPlayer] play];
        
        [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
            
        }];
        [OnlineAVplay shareAVPlayer].isPlay = YES;
    }

   [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scale= 1 - (scrollView.contentOffset.y/(self.hotSongListImageView.frame.size.height + 100) / 2);
    scale = (scale >= 1) ? scale : 1;
    self.hotSongListImageView.transform = CGAffineTransformMakeScale(scale, scale);
//    CGFloat offset=scrollView.contentOffset.y;
//    
//    if (offset<0) {
//        self.navigationController.navigationBar.barTintColor = [self.color colorWithAlphaComponent:0];
//    }else {
//        CGFloat alpha=1-((64-offset)/64);
//        self.navigationController.navigationBar.barTintColor=[self.color colorWithAlphaComponent:alpha];
//    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.section == 2|| self.section == 3) {
        return self.dataSource1.count;
    } else  {
       return self.dataSource.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XCShareMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCShareMusicTableViewCell"];
    
    
    if (self.section == 2 || self.section == 3) {
        
        XCHotSixSongListModel *hotSixSongListModel = self.dataSource1[indexPath.row];
        cell.hotSixSongListModel = hotSixSongListModel;
    } else if (self.section == 5) {
        XCInTheSongList *intheSonglist = self.dataSource[indexPath.row];
        cell.singerNameLable.text = intheSonglist.singerName;
        cell.nameLable.text = intheSonglist.name;
        [cell.picUrlImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[indexPath.row]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
    }
    cell.numberLable.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide / 5;
    
}
- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
